package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.SetmealMapper;
import org.dey.pojo.*;
import org.dey.service.MemberService;
import org.dey.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hg
 * @date 2022/3/15
 * @classname
 */
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    MemberService memberService;


    @Override
    public PageVo<Setmeal> setmealXx(Setmeal setmeal, int pageNum, int pagesize) {

        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<Setmeal>();
        if(StringUtils.isNotBlank(setmeal.getName())){
            queryWrapper.like("name",setmeal.getName());
        }

        PageHelper.startPage(pageNum, pagesize);

        List<Setmeal> list = this.list(queryWrapper);

        PageInfo<Setmeal> pageInfo = new PageInfo<Setmeal>(list);


        PageVo<Setmeal> pageVo = new PageVo<Setmeal>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;
    }

    @Override
    public Result zjSetmealXx(Setmeal setmeal) {
        //添加检查组时先查询数据库 检查组表最后一条数据 得到该检查组的编码

        Result result =null;

        Setmeal setmeal1 = setmealMapper.getSetmeal();

        String strcode = "";


        if(setmeal1.getCode()!=null&&setmeal1.getCode()!=""){
            String code = (Integer.parseInt(setmeal1.getCode())+1)+"";
            strcode = org.apache.commons.lang3.StringUtils.leftPad(code, 4, "0");

        }

        setmeal.setCode(strcode);

        boolean save = this.save(setmeal);

        if(save){
            result = new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return result;
    }

    @Override
    public Result scSetmealXx(int id) {
        Result result =null;

        boolean save = this.removeById(id);

        if(save){
            result = new Result(true, "删除套餐成功");
        }else {
            result = new Result(true,"删除套餐失败");
        }

        return result;
    }

    @Override
    public int xgSetmealXx(Setmeal setmeal) {
        boolean update = this.updateById(setmeal);
        return update==true?1:0;
    }

    @Override
    public Setmeal cxSetmealXxBYid(int id) {
        Setmeal query = this.getById(id);
        return query;
    }

    @Override
    public int tctjjcz(int id, CheckGroup[] checkGroups) {
        int addjcztjjcx = 0;

        float jiagehe = 0;
        for (CheckGroup item : checkGroups) {
            Integer id1 = item.getId();
            jiagehe = jiagehe + item.getPrice();
            addjcztjjcx = setmealMapper.tctjjcz(id, id1);
        }
        //添加检查项成功改变检查组的价格
        if(addjcztjjcx>=0){
            UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<Setmeal>();
            updateWrapper.eq("id",id).set("price",jiagehe);
            boolean update = this.update(updateWrapper);
            if(update){
                addjcztjjcx = 1;
            }
        }
        return addjcztjjcx;
    }

    @Override
    public List<CheckGroup> ckjianchazu(int id) {
        List<CheckGroup> list =  setmealMapper.ckjianchazu(id);
        return list;
    }

    @Override
    public List<Member> memberqb() {

        List<Member> list = memberService.list();

        return list;
    }

    @Override
    public List<Setmeal> Setmealqb() {
        List<Setmeal> list = this.list();
        return list;
    }
}
